<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 广告列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <!-- <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button> -->
                <!-- 公司名称:
                <el-input v-model="query.name" placeholder="公司名称" class="handle-input mr10"></el-input> -->
                广告资源类型:
                <el-select v-model="query.address" placeholder="全部" class="handle-select mr10">
                    <el-option key="-1" value="" label="全部"></el-option>
                    <el-option key="0" value="0" label="软文"></el-option>
                    <el-option key="1" value="1" label="主题展"></el-option>
                    <el-option key="2" value="2" label="公司"></el-option>
                    <el-option key="3" value="3" label="产品"></el-option>
                    <el-option key="4" value="4" label="图片"></el-option>
                    <el-option key="5" value="5" label="视频"></el-option>
                    <el-option key="6" value="6" label="H5链接"></el-option>
                </el-select>
                广告状态:
                <el-select v-model="query.adStatus" placeholder="全部" class="handle-select mr10">
                    <el-option key="0" label="全部" value=""></el-option>
                    <el-option key="1" label="启用" value="0"></el-option>
                    <el-option key="2" label="不启用" value="1"></el-option>
                </el-select>
                广告时间: &nbsp;从&nbsp;<el-date-picker v-model="query.time1" type="date" placeholder="选择日期"> </el-date-picker
                >&nbsp;到&nbsp;
                <el-date-picker v-model="query.time2" type="date" placeholder="选择日期"> </el-date-picker>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" style="display: block; margin: 10px 0px" @click="addAdvertisement">+ 新增广告</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="index" label="位置" width="50" align="center"> </el-table-column>
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <!-- <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column> -->
                <el-table-column label="广告图" width="300" align="center">
                    <template slot-scope="scope">
                        <el-image class="table-td-thumb" :src="scope.row.advertImg" :preview-src-list="[scope.row.advertImg]"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="二维码" width="150" align="center">
                    <template slot-scope="scope">
                        <el-image class="table-td-thumb-qrCode" :src="scope.row.qrCode" :preview-src-list="[scope.row.qrCode]"></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="showStartDate" label="广告时间" align="center"></el-table-column>
                <!-- <el-table-column label="账户余额">
                    <template slot-scope="scope">￥{{scope.row.money}}</template>
                </el-table-column> -->

                <el-table-column prop="showEndDate" label="展示时段" align="center"></el-table-column>
                <el-table-column prop="advertEnableStatus" label="广告状态" width="150" align="center"></el-table-column>
                <!-- <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.state==='成功'?'success':(scope.row.state==='失败'?'danger':'')"
                        >{{scope.row.state}}</el-tag>
                    </template>
                </el-table-column> -->

                <!-- <el-table-column prop="date" label="注册时间"></el-table-column> -->
                <el-table-column label="操作" align="center" width="300">
                    <template slot-scope="scope">
                        <el-button type="warning" @click="setStatus(scope.$index, scope.row)">
                            <span v-if="scope.row.advertEnableStatus === '不启动'">启用</span><span v-else>禁用</span>
                        </el-button>
                        <el-button type="primary" @click="toDetail(scope.$index, scope.row)">查看</el-button>
                        <el-button type="success" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <el-dialog title="广告详细资料" :visible.sync="adverVisible" width="50%">
            <el-form ref="form" :model="adverData" label-width="120px">
                <el-form-item label="广告图">
                    <el-image
                        style="height: 70px"
                        class="check_image"
                        :src="adverData.advertImg"
                        :preview-src-list="[adverData.advertImg]"
                    ></el-image>
                </el-form-item>
                <el-form-item label="二维码">
                    <el-image
                        style="width: 80px"
                        class="check_image"
                        :src="adverData.qrCode"
                        :preview-src-list="[adverData.qrCode]"
                    ></el-image>
                </el-form-item>
                <el-form-item label="广告资源类型">   
                        <el-radio key="0" label="0" v-model="adverData.advertType" disabled >>软文</el-radio>
                        <el-radio key="1" label="1" v-model="adverData.advertType" disabled >主题展</el-radio>
                        <el-radio key="2" label="2" v-model="adverData.advertType" disabled >公司</el-radio>
                        <el-radio key="3" label="3" v-model="adverData.advertType" disabled >产品</el-radio>
                        <el-radio key="4" label="4" v-model="adverData.advertType" disabled >图片</el-radio>
                        <el-radio key="5" label="5" v-model="adverData.advertType" disabled >视频</el-radio>
                        <el-radio key="6" label="6" v-model="adverData.advertType" disabled >产品链接</el-radio>
                </el-form-item>
                <el-form-item label="创建时间">
                    <el-input v-model="adverData.createTime" style="width: 250px"></el-input>
                </el-form-item>
                <el-form-item label="开始时间">
                    <el-input v-model="adverData.showStartDate" style="width: 250px"></el-input>
                </el-form-item>
                <el-form-item label="结束时间">
                    <el-input v-model="adverData.showEndDate" style="width: 250px"></el-input>
                </el-form-item>
                <el-form-item label="一级关联">
                    <el-input v-model="adverData.oneRelevantId" style="width: 250px"></el-input>
                </el-form-item>
                <el-form-item label="二级关联">
                    <el-input v-model="adverData.twoRelevantId" style="width: 250px"></el-input>
                </el-form-item>
                <el-form-item label="三级关联">
                    <el-input v-model="adverData.threeRelevantId" style="width: 250px"></el-input>
                </el-form-item>
                <el-form-item label="广告资源">
                    <el-input v-model="adverData.advertResource" style="width: 250px"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="adverVisible = false">取 消</el-button>
                <!-- <el-button type="primary" @click="saveEdit(form)">确 定</el-button> -->
            </span>
        </el-dialog>
    </div>
</template>

<script>
import axios from 'axios';
import moment from 'moment';
//import { fetchData } from '../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            // 文件标识
            AdImgMark: {
                fileGroup: 'advert',
                fileidentName: 'AdImgMark'
            },
            advertisementShow: false,
            form: {
                startTime: '',
                endTime: '',
                advertisementStatus: '',
                adImgSrc: ''
            },
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10,
                time1: '',
                time2: '',
                adStatus: ''
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            adverVisible: false,
            adverData: {},
            pageTotal: 1,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 新增按钮
        addAdvertisement() {
            this.$router.push('/advertisement-detail');
        },
        isProgramaType(type) {
            if (type == 0) {
                return '启动';
            }
            if (type == 1) {
                return '不启动';
            }
            // if(type==2){
            //     return '公司'
            // }
            // if(type==3){
            //     return '产品'
            // }
            // if(type==4){
            //     return '图片'
            // }
            // if(type==5){
            //     return '视频'
            // }
            // if(type==6){
            //     return 'H5链接'
            // }
        },
        // 修改状态
        setStatus(index, row) {
            console.log(row);
            if (row.advertEnableStatus === '不启动') {
                axios
                    .put(this.$myUrl.BASEURL + 'advert/updateAdvertEnableStatus?advertId=' + row.id + '&advertEnableStatus=0')
                    .then((res) => {
                        this.getData();
                    });
            } else {
                axios
                    .put(this.$myUrl.BASEURL + 'advert/updateAdvertEnableStatus?advertId=' + row.id + '&advertEnableStatus=1')
                    .then((res) => {
                        this.getData();
                    });
            }
        },
        toDetail(index, row) {
            this.adverVisible = true;
            
            axios.get(this.$myUrl.BASEURL + 'advert/getAdvert?advertId=' + row.id).then((res) => {
                //console.log(res)
                this.adverData = res.data.data;
                this.adverData.advertImg = this.$myUrl.IMGURL + this.adverData.advertImg;
                this.adverData.qrCode = this.$myUrl.IMGURL + this.adverData.qrCode;
                this.adverData.advertEnableStatus = this.isProgramaType(this.adverData.advertEnableStatus);
            });
        },
        getData() {
            axios.get(this.$myUrl.BASEURL + 'advert/getAdvertList?pageNum=0&pageSize=0').then((res) => {
                console.log('=========================', res);
                this.tableData = res.data.data;

                this.pageTotal = res.data.totalNumber;
                this.tableData.forEach((item, index) => {
                    ////console.log(item)
                    //item.showStartDate.trim().split(" ")
                    var s = item.showStartDate.trim().split(' ');
                    var e = item.showEndDate.trim().split(' ');

                    item.showStartDate = s[0] + ' 到 ' + e[0];
                    //console.log(s)
                    //console.log(e)
                    if (s[1] == '00:00:00' && e[1] == '00:00:00') {
                        item.showEndDate = '全天';
                    } else if (s[1] != '00:00:00' || e != '00:00:00') {
                        item.showEndDate = s[1] + ' 到 ' + e[1];
                    }
                    item.advertImg = this.$myUrl.IMGURL + item.advertImg;
                    item.qrCode = this.$myUrl.IMGURL + item.qrCode;
                    item.advertEnableStatus = this.isProgramaType(item.advertEnableStatus);
                });
            });
        },
        // 触发搜索按钮
        handleSearch() {
            let startTime = moment(this.query.time1).format('YYYY-MM-DD');
            let endTime = moment(this.query.time2).format('YYYY-MM-DD');
            // console.log(moment(this.query.time1).format('YYYY-MM-DD'));
            // console.log(moment(this.query.time2).format('YYYY-MM-DD'));
            // console.log();
            if (startTime == 'Invalid date') {
                startTime = '';
            }
            if (endTime == 'Invalid date') {
                endTime = '';
            }
            // console.log(startTime,endTime);
            // 状态
            // console.log(this.query.address);
            // this.$set(this.query, 'pageIndex', 1);
            // this.getData();
            axios
                .get(
                    this.$myUrl.BASEURL +
                        'advert/getAdvertList?pageNum=' +
                        this.query.pageIndex +
                        '&pageSize=' +
                        this.query.pageSize +
                        '&advertType=' +
                        this.query.address +
                        '&advertEnableStatus=' +
                        this.query.adStatus +
                        '&showStartTime=' +
                        startTime +
                        '&showEndTime=' +
                        endTime
                )
                .then((res) => {
                    console.log(res);
                    this.tableData = res.data.data;
                    this.pageTotal = res.data.totalNumber;

                    this.tableData.forEach((item, index) => {
                        ////console.log(item)
                        //item.showStartDate.trim().split(" ")
                        var s = item.showStartDate.trim().split(' ');
                        var e = item.showEndDate.trim().split(' ');

                        item.showStartDate = s[0] + ' 到 ' + e[0];
                        //console.log(s)
                        //console.log(e)
                        if (s[1] == '00:00:00' && e[1] == '00:00:00') {
                            item.showEndDate = '全天';
                        } else if (s[1] != '00:00:00' || e != '00:00:00') {
                            item.showEndDate = s[1] + ' 到 ' + e[1];
                        }
                        item.advertImg = this.$myUrl.IMGURL + item.advertImg;
                        item.qrCode = this.$myUrl.IMGURL + item.qrCode;
                        item.advertEnableStatus = this.isProgramaType(item.advertEnableStatus);
                    });
                });
        },
        // 删除操作
        handleDelete(index, row) {
            console.log(row);
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    axios.delete(this.$myUrl.BASEURL + 'advert/deleteAdvert?advertId=' + row.id).then((res) => {
                        if (res.data.code == 200) {
                            this.$message.success('删除成功');
                            this.getData();
                        }
                    });
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            console.log(row);
            this.idx = index;
            this.form = row;
            // this.editVisible = true;
            this.$router.push('/advertisement-detail?adId=' + row.id);
        },
        // 保存编辑和新增
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        onSubmit() {
            this.advertisementShow = false;
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.mr11 {
    width: 100px;
    margin: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 95%;
    height: 95%;
}
.table-td-thumb-qrCode {
    width: 50%;
    height: 50%;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
video {
    width: 100%;
    height: 100%;
}
</style>
